package com.bsh.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import com.bsh.mybatis.IRepository.AdvertMapper;
import com.bsh.mybatis.model.Advert;

@Repository
public class DaoAdvert{ 
	@Resource
	SqlSessionTemplate session; 

	public int deleteByPrimaryKey(Integer id) {
		AdvertMapper map = session.getMapper(AdvertMapper.class);
		int res = map.deleteByPrimaryKey(id);
		return res;
	}

	public int insert(Advert record) {
		AdvertMapper map = session.getMapper(AdvertMapper.class);
		int res = map.insert(record);
		return res;
	}

	public int insertSelective(Advert record) {
		AdvertMapper map = session.getMapper(AdvertMapper.class);
		int res = map.insertSelective(record);
		return res;
	}

	public Advert selectByPrimaryKey(Integer id) {
		AdvertMapper map = session.getMapper(AdvertMapper.class);
		Advert row = map.selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(Advert record) {
		AdvertMapper map = session.getMapper(AdvertMapper.class);
		int res = map.updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(Advert record) {
		AdvertMapper map = session.getMapper(AdvertMapper.class);
		int res = map.updateByPrimaryKey(record);
		return res;
	}
	

	//查询列表 --- 自定义的查询方法
	public List<Advert> selectList(int id) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		List<Advert> list = dao.selectList(34);
		return list;
	}

	//直接执行sql语句测试
	public List<Advert> sqlQuery(String sql) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		List<Advert> list = dao.sqlQuery("select * from advert where id=34");
		return list;
	}

	//直接执行sql语句测试 -- 构建复杂查询
	public List<Advert> sqlQueryComp(int id) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		List<Advert> list = dao.sqlQueryComp(id);
		return list;
	}

	//直接执行sql语句测试 -- 返回HashMap
	public HashMap sqlQueryMap(String sql) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		HashMap list = dao.sqlQueryMap("select * from advert where id=34");
		return list;
	}

	//直接执行sql语句测试 -- 返回List<HashMap>
	public List<Map> sqlQueryListMap(String sql) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		List<Map> list = dao.sqlQueryListMap("select * from advert");
		return list;
	}
	
	//参数化查询 -- 多参数查询
	public List<Map> sqlQueryListMapByParams(HashMap<String,Object> params) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		
		//参数字典
		params=new HashMap<String, Object>();
		params.put("id",34);
		
		//调用dao，传入参数字典
		List<Map> list = dao.sqlQueryListMapByParams(params);	
		
		return list;
	}
	
	//分页查询 -- 多参数查询
	public List<Map> getPage(HashMap<String, Object> params) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		
		//参数字典
		params=new HashMap<String, Object>();
		params.put("start",0);
		params.put("end",2);
		
		//调用dao，传入参数字典
		List<Map> list = dao.getPage(params);	
		return list;
	}	
	
	//参数字典类型定义，动态构建sql语句, --查询结果为List<Map>
	public List<Map> sqlQueryByParamMap(HashMap<String, Object> params) {
		AdvertMapper dao = session.getMapper(AdvertMapper.class);
		
		//参数字典
		params=new HashMap<String, Object>();
		params.put("id",34);
		
		//调用dao，传入参数字典
		List<Map> list = dao.sqlQueryByParamMap(params);	
		
		return list;
	}		
}
